package com.zx.map.service.impl;

import com.zx.map.dao.ShareProfitDetailLogDao;
import com.zx.map.dao.model.ShareProfitDetailLog;
import com.zx.map.dao.model.ShareProfitDetailLogExample;
import com.zx.map.service.AgentInfoService;
import com.zx.map.service.MerchantInfoService;
import com.zx.map.service.ShareProfitDetailLogService;
import com.zx.map.service.model.AgentInfoModel;
import com.zx.map.service.model.MerchantInfoModel;
import com.zx.map.service.model.ShareProfitDetailLogModel;
import com.zx.map.utils.MyBeanUtils;
import com.zx.mobile.domain.ShareProfitDetailDomain;
import com.zx.mobile.domain.SpDetailDomain;
import com.zx.platform.dao.model.Roles;
import com.zx.platform.service.DirectoryService;
import com.zx.platform.service.RolesService;
import com.zx.platform.utils.PmpUtils;
import org.apache.commons.lang3.StringUtils;
import org.apache.log4j.Logger;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.dao.DataAccessException;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Created by IntelliJ IDEA. User: Administrator Date: 14-3-1 Time: 上午11:07 To
 * change this template use File | Settings | File Templates.
 */
@Service
public class ShareProfitDetailLogServiceImpl implements ShareProfitDetailLogService {
    /**
     * log
     */
    private static Logger log = Logger
            .getLogger(ShareProfitDetailLogServiceImpl.class);

    @Autowired
    private ShareProfitDetailLogDao dao;
    @Autowired
    private AgentInfoService agentInfoService;
    @Autowired
    private RolesService rolesService;
    @Autowired
    private MerchantInfoService merchantInfoService;

    @Autowired
    private DirectoryService directoryService;

    /**
     * 根据Model查找列表
     */
    public List<ShareProfitDetailLogModel> getListByModel(SpDetailDomain spDetailDomain)
            throws DataAccessException {
        ShareProfitDetailLogExample example = new ShareProfitDetailLogExample();
        ShareProfitDetailLogExample.Criteria criteria = example.createCriteria();
        if (StringUtils.isNotBlank(spDetailDomain.getSysAccount())) {
            criteria.andAgentSysAccountEqualTo(spDetailDomain.getSysAccount());
        }
        if (StringUtils.isNotBlank(spDetailDomain.getBeginDate())) {
            criteria.andCreateDateBetween(PmpUtils.strToDate(spDetailDomain.getBeginDate(), "yyyy-MM-dd"),
                    PmpUtils.addDate(PmpUtils.strToDate(spDetailDomain.getEndDate(), "yyyy-MM-dd"), 1));
        }
        example.setOrderByClause("CREATE_DATE DESC");
        List<ShareProfitDetailLog> list = dao.getListByExample(example);
        List<ShareProfitDetailLogModel> modelList = new ArrayList<ShareProfitDetailLogModel>();
        for (ShareProfitDetailLog o : list) {
            ShareProfitDetailLogModel temp = new ShareProfitDetailLogModel();
            BeanUtils.copyProperties(o, temp);
            modelList.add(temp);
        }
        return modelList;
    }

    /**
     * 新增
     */
    public String add(ShareProfitDetailLogModel model) throws DataAccessException {
        ShareProfitDetailLog temp = new ShareProfitDetailLog();
        BeanUtils.copyProperties(model, temp);
        String key = dao.add(temp);
        return key;
    }

    /**
     * 修改
     */
    public String update(ShareProfitDetailLogModel model) throws DataAccessException {
        ShareProfitDetailLog temp = new ShareProfitDetailLog();
        BeanUtils.copyProperties(model, temp);
        return dao.update(temp);
    }

    /**
     * 删除
     */
    public void delete(ShareProfitDetailLogModel model) throws DataAccessException {
        ShareProfitDetailLogExample example = new ShareProfitDetailLogExample();
        ShareProfitDetailLogExample.Criteria criteria = example.createCriteria();
        if (StringUtils.isNotBlank(model.getSpDetailDay())) {
            criteria.andSpDetailDayEqualTo(model.getSpDetailDay());
        }
        dao.delete(example);
    }

    /**
     * 代理商收益明细分页查询
     * @throws DataAccessException
     */
    public Map getPageByDomain(AgentInfoModel curAgentInfo,AgentInfoModel searchAgentInfoModel,MerchantInfoModel searchMerchantInfoModel, ShareProfitDetailDomain shareProfitDetailDomain)
            throws DataAccessException {
        Map<String, Object> map = new HashMap<String, Object>();
        ShareProfitDetailLogExample example=initQueryExample(curAgentInfo,searchAgentInfoModel,searchMerchantInfoModel,shareProfitDetailDomain);

        int pageBegin = (shareProfitDetailDomain.getPageNum()-1)*shareProfitDetailDomain.getPageSize();

        List<ShareProfitDetailLog> list = dao.getPageByExample(shareProfitDetailDomain.getPageSize(), pageBegin, example);
        List<ShareProfitDetailLogModel> modelList = new ArrayList<ShareProfitDetailLogModel>();
        //组装信息
        for (ShareProfitDetailLog oo : list) {
            ShareProfitDetailLogModel shareProfitDetailLogModelTmp=new ShareProfitDetailLogModel();
            MyBeanUtils.map(oo,shareProfitDetailLogModelTmp);
            MerchantInfoModel merchantTemp = merchantInfoService.getModelByKey(oo.getMerchantId());
            shareProfitDetailLogModelTmp.setMerchantName(merchantTemp.getMerchantName());
            shareProfitDetailLogModelTmp.setMerSysAccount(merchantTemp.getSysAccount());

            AgentInfoModel agentInfoModelTemp = agentInfoService.getModelByKey(oo.getAgentId());
            shareProfitDetailLogModelTmp.setAgentName(agentInfoModelTemp.getAgentName());
            shareProfitDetailLogModelTmp.setAgentSysAccount(agentInfoModelTemp.getSysAccount());

            Roles rolesTemp=rolesService.getLevelNameByRoleId(agentInfoModelTemp.getAgentLevel());
            shareProfitDetailLogModelTmp.setRoleName(rolesTemp.getRoleName());


            String shareProfitTypeName=directoryService.getShareProfitTypeNameByType(oo.getSpDetailType());
            shareProfitDetailLogModelTmp.setSpDetailTypeName(shareProfitTypeName);
            modelList.add(shareProfitDetailLogModelTmp);
        }

        int count = dao.countListByExample(example);
        map.put("count", count);
        map.put("list", modelList);
        return map;
    }

    /**
     * 我的收益分页查询
     * @return
     * @throws DataAccessException
     */
    @Override
    public Map getMyShareProfitPageByDomain(AgentInfoModel curAgentInfoModel, AgentInfoModel searchAgentInfoModel, MerchantInfoModel searchMerchantInfoModel, ShareProfitDetailDomain domain) throws DataAccessException {
        Map<String, Object> map = new HashMap<String, Object>();
        ShareProfitDetailLogExample example=myShareProfitExample(curAgentInfoModel,searchAgentInfoModel,searchMerchantInfoModel,domain);

        int pageBegin = (domain.getPageNum()-1)*domain.getPageSize();

        List<ShareProfitDetailLog> list = dao.getPageByExample(domain.getPageSize(), pageBegin, example);
        List<ShareProfitDetailLogModel> modelList = new ArrayList<ShareProfitDetailLogModel>();
        //组装信息
        for (ShareProfitDetailLog oo : list) {
            ShareProfitDetailLogModel shareProfitDetailLogModelTmp=new ShareProfitDetailLogModel();
            MyBeanUtils.map(oo,shareProfitDetailLogModelTmp);
            MerchantInfoModel merchantTemp = merchantInfoService.getModelByKey(oo.getMerchantId());
            shareProfitDetailLogModelTmp.setMerchantName(merchantTemp.getMerchantName());
            shareProfitDetailLogModelTmp.setMerSysAccount(merchantTemp.getSysAccount());

            AgentInfoModel agentInfoModelTemp = agentInfoService.getModelByKey(oo.getAgentId());
            shareProfitDetailLogModelTmp.setAgentName(agentInfoModelTemp.getAgentName());
            shareProfitDetailLogModelTmp.setAgentSysAccount(agentInfoModelTemp.getSysAccount());

            Roles rolesTemp=rolesService.getLevelNameByRoleId(agentInfoModelTemp.getAgentLevel());
            shareProfitDetailLogModelTmp.setRoleName(rolesTemp.getRoleName());

            String shareProfitTypeName=directoryService.getShareProfitTypeNameByType(oo.getSpDetailType());
            shareProfitDetailLogModelTmp.setSpDetailTypeName(shareProfitTypeName);
            modelList.add(shareProfitDetailLogModelTmp);
        }
        int count = dao.countListByExample(example);
        map.put("count", count);
        map.put("list", modelList);
        return map;
    }

    @Override
    public Double queryTotalSpDetailAmtByExample(AgentInfoModel curAgentInfo,AgentInfoModel searchAgentInfoModel,MerchantInfoModel searchMerchantInfoModel,ShareProfitDetailDomain domain){
        ShareProfitDetailLogExample example = shareExample(curAgentInfo, searchAgentInfoModel, searchMerchantInfoModel, domain);
        Double totalSpDetail =  dao.queryTotalSpDetailAmtByExample(example);

        if(totalSpDetail==null){
            return  0d;
        }else{
            return totalSpDetail;
        }
    }

    /**
     * 我的收益-期间收益查询条件
     */
    private ShareProfitDetailLogExample shareExample(AgentInfoModel curAgentInfo, AgentInfoModel searchAgentInfoModel, MerchantInfoModel searchMerchantInfoModel, ShareProfitDetailDomain shareProfitDetailDomain) {
        //符合商户条件id列表
        List<String> merchantIdList = merchantInfoService.queryListMerchantIdByMerchantName(searchMerchantInfoModel);
        ShareProfitDetailLogExample example = new ShareProfitDetailLogExample();
        ShareProfitDetailLogExample.Criteria criteria = example.createCriteria();
        //商户条件过滤
        if (merchantIdList == null || merchantIdList.isEmpty()) {
            merchantIdList.add("-1");
            log.debug("商户空");
        }
        criteria.andMerchantIdIn(merchantIdList);
        if(StringUtils.isNotBlank(curAgentInfo.getAgentId())){
            criteria.andAgentIdEqualTo(curAgentInfo.getAgentId());
        }
        if (StringUtils.isNotBlank(shareProfitDetailDomain.getBeginDate())) {
            criteria.andCreateDateBetween(PmpUtils.strToDate(shareProfitDetailDomain.getBeginDate(), "yyyy-MM-dd"),
                    PmpUtils.addDate(PmpUtils.strToDate(shareProfitDetailDomain.getEndDate(), "yyyy-MM-dd"), 1));
        }
        if(StringUtils.isNotBlank(shareProfitDetailDomain.getMerSysAccount())){
            criteria.andMerSysAccountLike("%"+shareProfitDetailDomain.getMerSysAccount()+"%");
        }
        if(StringUtils.isNotBlank(shareProfitDetailDomain.getSpDetailType())){
            criteria.andSpDetailTypeEqualTo(shareProfitDetailDomain.getSpDetailType());
        }
        example.setOrderByClause("CREATE_DATE DESC");
        return example;
    }

    private ShareProfitDetailLogExample initQueryExample(AgentInfoModel curAgentInfo, AgentInfoModel searchAgentInfoModel, MerchantInfoModel searchMerchantInfoModel, ShareProfitDetailDomain shareProfitDetailDomain) {
        //符合商户条件id列表
        List<String> merchantIdList = merchantInfoService.queryListMerchantIdByAgentInfo(curAgentInfo,searchMerchantInfoModel);
        //代理商下级id列表
        List<String> agentIdList = agentInfoService.queryDownAgentIdList(curAgentInfo,searchAgentInfoModel);
        ShareProfitDetailLogExample example = new ShareProfitDetailLogExample();
        ShareProfitDetailLogExample.Criteria criteria = example.createCriteria();
        if (agentIdList == null || agentIdList.isEmpty()) {
            agentIdList.add("-1");
            log.debug("下级代理空");
        }
        log.debug("下级代理长度"+agentIdList.size()+" "+agentIdList.toString());
        //代理商条件过滤
        criteria.andAgentIdIn(agentIdList);

        //商户条件过滤
        if (merchantIdList == null || merchantIdList.isEmpty()) {
            merchantIdList.add("-1");
            log.debug("商户空");
        }
        criteria.andMerchantIdIn(merchantIdList);

        if (StringUtils.isNotBlank(shareProfitDetailDomain.getAgentSysAccount())) {
            criteria.andAgentSysAccountLike("%"+shareProfitDetailDomain.getAgentSysAccount()+"%");
        }

        if (StringUtils.isNotBlank(shareProfitDetailDomain.getBeginDate())) {
            criteria.andCreateDateBetween(PmpUtils.strToDate(shareProfitDetailDomain.getBeginDate(), "yyyy-MM-dd"),
                    PmpUtils.addDate(PmpUtils.strToDate(shareProfitDetailDomain.getEndDate(), "yyyy-MM-dd"), 1));
        }
        if(StringUtils.isNotBlank(shareProfitDetailDomain.getMerSysAccount())){
            criteria.andMerSysAccountLike("%"+shareProfitDetailDomain.getMerSysAccount()+"%");
        }
        if(StringUtils.isNotBlank(shareProfitDetailDomain.getSpDetailType())){
            criteria.andSpDetailTypeEqualTo(shareProfitDetailDomain.getSpDetailType());
        }
        example.setOrderByClause("CREATE_DATE DESC");
        return example;
    }

    private ShareProfitDetailLogExample myShareProfitExample(AgentInfoModel curAgentInfo, AgentInfoModel searchAgentInfoModel, MerchantInfoModel searchMerchantInfoModel, ShareProfitDetailDomain shareProfitDetailDomain) {
        //符合商户条件id列表
        List<String> merchantIdList = merchantInfoService.queryListMerchantIdByMerchantName(searchMerchantInfoModel);
        ShareProfitDetailLogExample example = new ShareProfitDetailLogExample();
        ShareProfitDetailLogExample.Criteria criteria = example.createCriteria();
        //商户条件过滤
        if (merchantIdList == null || merchantIdList.isEmpty()) {
            merchantIdList.add("-1");
            log.debug("商户空");
        }
        criteria.andMerchantIdIn(merchantIdList);

        if (StringUtils.isNotBlank(curAgentInfo.getAgentId())) {
            criteria.andAgentIdEqualTo(curAgentInfo.getAgentId());
        }
        if (StringUtils.isNotBlank(shareProfitDetailDomain.getBeginDate())) {
            criteria.andCreateDateBetween(PmpUtils.strToDate(shareProfitDetailDomain.getBeginDate(), "yyyy-MM-dd"),
                    PmpUtils.addDate(PmpUtils.strToDate(shareProfitDetailDomain.getEndDate(), "yyyy-MM-dd"), 1));
        }
        if(StringUtils.isNotBlank(shareProfitDetailDomain.getMerSysAccount())){
            criteria.andMerSysAccountLike("%"+shareProfitDetailDomain.getMerSysAccount()+"%");
        }
        if(StringUtils.isNotBlank(shareProfitDetailDomain.getSpDetailType())){
            criteria.andSpDetailTypeEqualTo(shareProfitDetailDomain.getSpDetailType());
        }
        example.setOrderByClause("CREATE_DATE DESC");
        return example;
    }

}
